<?php
namespace app\admin\model;
use app\admin\BaseModel;
use think\model\concern\SoftDelete;

class Goods extends BaseModel
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    public static function getGoodsInfo($id,$field = true,$where = [])
    {
        return Goods::where($where)->field($field)->find($id)->toArray();
    }

    public static function goodsAdd($data)
    {

        $add=Goods::create($data);
        return $add;
    }

    public function searchKeywordsAttr($q,$v,$d)
    {
        $q->where('g.name','like',"%{$v}%");
    }

    public function searchIsDownAttr($q,$v,$d)
    {
        $q->where('g.is_down','=',$v);
    }

    public function searchIsShowAttr($q,$v,$d)
    {
        $q->where('g.is_show','=',$v);
    }

    public function searchIsHotAttr($q,$v,$d)
    {
        $q->where('g.is_hot','=',$v);
    }

    public function searchCidAttr($q,$v,$d)
    {
        $q->where('g.cid','=',$v);
    }

    public function getAddtimeAttr($v,$d)
    {
        $v = date('Y-m-d H:i:s',$v);
        return $v;
    }

    public static function getGoodsList($where)
    {
        return Goods::alias('g')
            ->withSearch(array_keys($where),$where)
            ->join('category c','g.cid=c.id')
            ->field('g.id,g.name,g.photo_x,g.price,g.is_hot,g.is_show,g.is_down,g.addtime,c.name cname')
            ->order('g.id desc')
            ->paginate([
                'list_rows'=>5,
                'query'   => $where
            ]);
    }
}